use serde::{Deserialize, Serialize};

use async_graphql::{Context, ID};
use derivative::Derivative;

use crate::features::gfilter::StringExactFilterIn;

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone)]
#[graphql(complex)]
#[derive(Derivative)]
#[derivative(Default)]
/// this is Word
pub struct Word {
  pub id: ID,
  /// 单词名称
  pub word: String,
  /// 单词所属语言
  pub lang: String,

  /// 单词释义
  pub definitions: Option<Vec<String>>,

  /// 字符串标签：zk/中考，gk/高考，cet4/四级 等等标签，空格分割
  pub tags: Option<Vec<String>>,

  /// 时态复数等变换
  pub en_exchanges: Option<Vec<WordExchange>>,

  /// [英语]音标，以英语英标为主
  pub en_phonetic: Option<String>,
  /// [英语]词语位置，用 "/" 分割不同位置
  pub en_pos: Option<String>,
  /// [英语]柯林斯星级
  pub en_collins: Option<String>,
  /// [英语]是否是牛津三千核心词汇
  pub en_oxford: Option<String>,
  /// [英语]英国国家语料库词频顺序
  pub en_bnc: Option<String>,
  /// [英语]当代语料库词频顺序
  pub en_frq: Option<String>,

  /// [中文] 拼音
  pub zh_pinyin: Option<String>,
  /// [中文] 笔画
  pub zh_strokes: Option<String>,

  pub created_at: Option<i64>,
  pub updated_at: Option<i64>,
}

#[async_graphql::ComplexObject]
impl Word {
  pub async fn from(&self) -> String {
    let mut from = String::new();
    from.push_str(&self.id);
    from.push_str("<");
    from.push_str(&self.word);
    from.push_str(">");

    from
  }

  /// 单词翻译释义
  pub async fn translation(&self, ctx: &Context<'_>, lang: StringExactFilterIn) -> Vec<String> {
    vec![format!("单词释义，每行一个释义 for target lang={:?}", lang)]
  }
}

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone)]
#[graphql(complex)]
#[derive(Derivative)]
#[derivative(Default)]
/// this is Word Exchange
pub struct WordExchange {
  pub id: ID,

  ///关联的单词
  pub word: Word,

  /// p	过去式（did）
  /// d	过去分词（done）
  /// i	现在分词（doing）
  /// 3	第三人称单数（does）
  /// r	形容词比较级（-er）
  /// t	形容词最高级（-est）
  /// s	名词复数形式
  /// 0	Lemma，如 perceived 的 Lemma 是 perceive
  /// 1	Lemma 的变换形式，比如 s 代表 apples 是其 lemma 的复数形式
  pub k: String,
  /// 对应单词变形如：d:perceived/p:perceived/3:perceives/i:perceiving
  pub v: String,

  pub created_at: Option<i64>,
  pub updated_at: Option<i64>,
}

#[async_graphql::ComplexObject]
impl WordExchange {
  pub async fn from(&self) -> String {
    let mut from = String::new();
    from.push_str(&self.id);
    from.push_str("<");
    from.push_str(&self.word.id);
    from.push_str(">");

    from
  }
}

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone)]
#[graphql(complex)]
#[derive(Derivative)]
#[derivative(Default)]
/// this is Sentence
pub struct Sentence {
  pub id: ID,

  /// 句子所属的语言
  pub lang: String,
  /// 句子内容
  pub value: String,

  pub created_at: Option<i64>,
  pub updated_at: Option<i64>,
}

#[async_graphql::ComplexObject]
impl Sentence {
  pub async fn from(&self) -> String {
    let mut from = String::new();
    from.push_str(&self.id);
    from.push_str("<");
    from.push_str(&self.lang);
    from.push_str(">");

    from
  }

  ///关联的单词
  pub async fn words(&self, ctx: &Context<'_>) -> Vec<Word> {
    vec![]
  }

  /// 句子短文翻译释义
  pub async fn translation(&self, ctx: &Context<'_>, lang: StringExactFilterIn) -> Vec<String> {
    vec![format!("句子短文翻译释义 for target lang={:?}", lang)]
  }
}
